﻿Public Class ctrDiaAtendimento
    Inherits ctrPadrao

#Region "Declaração de variáveis"
    Private objDiaAtendimento As New Persistencia.perDiaAtendimento
    Private iIdDiaAtendimento As Int32
#End Region

#Region "Propriedades"

    Public Property IDDiaAtendimento() As Int32
        Get
            Return Me.iIdDiaAtendimento
        End Get
        Set(ByVal value As Int32)
            Me.iIdDiaAtendimento = value
        End Set
    End Property

#End Region

#Region "Métodos públicos"

    Public Function salvar(ByVal iIdDiaAtendimento As Int32, _
                           ByVal bSegunda As Boolean, _
                           ByVal bTerca As Boolean, _
                           ByVal bQuarta As Boolean, _
                           ByVal bQuinta As Boolean, _
                           ByVal bSexta As Boolean, _
                           ByVal bSabado As Boolean) As Boolean

        Dim bResultado As Boolean = False

        If iIdDiaAtendimento > 0 Then
            Me.alterarDiaAtendimento(iIdDiaAtendimento, _
                                     bSegunda, _
                                     bTerca, _
                                     bQuarta, _
                                     bQuinta, _
                                     bSexta, _
                                     bSabado)
            Me.IDDiaAtendimento = iIdDiaAtendimento
            bResultado = True
        Else
            Me.IDDiaAtendimento = Me.inserirDiaAtendimento(bSegunda, _
                                                           bTerca, _
                                                           bQuarta, _
                                                           bQuinta, _
                                                           bSexta, _
                                                           bSabado)

            bResultado = True
        End If

        Return bResultado

    End Function

    Public Function excluir(ByVal iIdDiaAtendimento As Int32) As Boolean
        Dim bResultado As Boolean = True

        If bResultado Then
            Me.excluirDiaAtendimento(iIdDiaAtendimento)
        End If

        Return bResultado

    End Function

    Private Sub excluirDiaAtendimento(ByVal iIdDiaAtendimento As Int32)

        Try
            Me.objDiaAtendimento.excluirDiaAtendimento(iIdDiaAtendimento)
        Catch ex As Exception
            Throw New Exception("Exceção no método 'excluirDiaAtendimento'. " & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try

    End Sub

    Public Function selecionarDiaAtendimento(ByVal iIdDiaAtendimento As Int32) As DataRow

        Try
            Return Me.objDiaAtendimento.selecionarDiaAtendimento(iIdDiaAtendimento)
        Catch ex As Exception
            Throw New Exception("Exceção no método 'selecionarDiaAtendimento'. " & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try

    End Function

    Public Function retornarCampoDiaAtendimento(ByVal sCampo As String, ByVal iIDDiaAtendimento As Int32) As DataRow

        Try
            Dim dtDados As New DataTable
            Dim drRetorno As DataRow

            dtDados = Me.objDiaAtendimento.selecionarCampoDiaAtendimento(sCampo, iIDDiaAtendimento)

            If Not dtDados Is Nothing Then
                drRetorno = dtDados.Rows(0)
            Else
                drRetorno = Nothing
            End If

            Return drRetorno

        Catch ex As Exception
            Throw New Exception("Exceção no método 'retornarCampoDiaAtendimento'. " & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try

    End Function

#End Region

#Region "Métodos privados"

    Private Sub alterarDiaAtendimento(ByVal iIdDiaAtendimento As Int32, _
                                      ByVal bSegunda As Boolean, _
                                      ByVal bTerca As Boolean, _
                                      ByVal bQuarta As Boolean, _
                                      ByVal bQuinta As Boolean, _
                                      ByVal bSexta As Boolean, _
                                      ByVal bSabado As Boolean)
        Try
            Me.objDiaAtendimento.alterarDiaAtendimento(iIdDiaAtendimento, _
                                                       bSegunda, _
                                                       bTerca, _
                                                       bQuarta, _
                                                       bQuinta, _
                                                       bSexta, _
                                                       bSabado)
        Catch ex As Exception
            Throw New Exception("Exceção no método 'alterarDiaAtendimento'. " & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try

    End Sub

    Private Function inserirDiaAtendimento(ByVal bSegunda As Boolean, _
                                           ByVal bTerca As Boolean, _
                                           ByVal bQuarta As Boolean, _
                                           ByVal bQuinta As Boolean, _
                                           ByVal bSexta As Boolean, _
                                           ByVal bSabado As Boolean) As Integer
        Dim iIdDiaAtendimento As Int32

        Try
            iIdDiaAtendimento = Me.objDiaAtendimento.inserirDiaAtendimento(bSegunda, _
                                                                           bTerca, _
                                                                           bQuarta, _
                                                                           bQuinta, _
                                                                           bSexta, _
                                                                           bSabado)
            Return iIdDiaAtendimento
        Catch ex As Exception
            Throw New Exception("Exceção no método 'inserirDiaAtendimento'. " & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try

    End Function
    Private Function GerarDataset() As DataSet
        Dim dsTemp As New DataSet

        Try
            With dsTemp
                .Tables.Add("DiasSugestao")
                .Tables("DiasSugestao").Columns.Add("Data", System.Type.GetType("System.DateTime"), "")
                .Tables("DiasSugestao").Columns.Add("DiaSemana", System.Type.GetType("System.String"), "")
            End With

            Return dsTemp

        Catch ex As Exception
            Throw New Exception("Exceção no método 'GerarDataset'." & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try

    End Function
    Public Function retornarDiasSugeridos(ByVal dtDataBase As Date, ByVal iIDRegiao As Int32) As DataSet
        Dim dsDiasSugeridos As New DataSet
        Dim drDia As DataRow
        Dim drDiasRegiao As DataRow
        Dim cont As Integer
        Dim dtDataVer As Date
        Dim dtData As Date
        Dim sDiaDaSemana As String

        Try
            dsDiasSugeridos = GerarDataset()

            drDiasRegiao = objDiaAtendimento.AtendimentoPorRegiao(iIDRegiao)
            dtDataVer = dtDataBase
            'Trocar para sugestao de mais dias, por isso for de 1 a 1
            For cont = 1 To 1
                While dsDiasSugeridos.Tables("DiasSugestao").Rows.Count = 0
                    dtDataVer = DateAdd(DateInterval.Day, 1, dtDataVer)
                    dtData = Date.MinValue
                    Select Case dtDataVer.DayOfWeek
                        Case DayOfWeek.Monday
                            If (Persistencia.perConversao.nuloParaBoleano(drDiasRegiao.Item("Segunda"))) Then
                                dtData = dtDataVer
                                sDiaDaSemana = "Segunda-feira"
                            End If
                        Case DayOfWeek.Tuesday
                            If (Persistencia.perConversao.nuloParaBoleano(drDiasRegiao.Item("Terca"))) Then
                                dtData = dtDataVer
                                sDiaDaSemana = "Terça-feira"
                            End If
                        Case DayOfWeek.Wednesday
                            If (Persistencia.perConversao.nuloParaBoleano(drDiasRegiao.Item("Quarta"))) Then
                                dtData = dtDataVer
                                sDiaDaSemana = "Quarta-feira"
                            End If
                        Case DayOfWeek.Thursday
                            If (Persistencia.perConversao.nuloParaBoleano(drDiasRegiao.Item("Quinta"))) Then
                                dtData = dtDataVer
                                sDiaDaSemana = "Quinta-feira"
                            End If
                        Case DayOfWeek.Friday
                            If (Persistencia.perConversao.nuloParaBoleano(drDiasRegiao.Item("Sexta"))) Then
                                dtData = dtDataVer
                                sDiaDaSemana = "Sexta-feira"
                            End If
                        Case DayOfWeek.Saturday
                            If (Persistencia.perConversao.nuloParaBoleano(drDiasRegiao.Item("Sabado"))) Then
                                dtData = dtDataVer
                                sDiaDaSemana = "Sábado"
                            End If
                    End Select
                    If (dtData <> Date.MinValue) Then
                        With dsDiasSugeridos.Tables("DiasSugestao")
                            drDia = .NewRow()
                            drDia.Item("Data") = dtData.Date
                            drDia.Item("DiaSemana") = dtData.DayOfWeek
                            .Rows.Add(drDia)
                        End With
                    End If
                End While
            Next

        Catch ex As Exception
            Throw New Exception("Exceção no método 'retornarDiasSugeridos'." & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try

        Return dsDiasSugeridos

    End Function

#End Region
End Class
